package com.chen.lifepic.LargeViewFragment;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.chen.lifepic.DataInfo.DataInfo;
import com.chen.lifepic.thumbnailManager.IImageReadyListener;
import com.chen.lifepic.thumbnailManager.ThumbnailManager;
import com.chen.lifepic.utils.APPCommonDef.PictureType;

public class LargeSubView extends ImageView implements
		IImageReadyListener {
	DataInfo _info = null;

	public LargeSubView(Context context) {
		super(context);
		ThumbnailManager.getInstance().registerImageReadyListener(this);
	}

	public LargeSubView(Context context, AttributeSet attrs) {
		super(context, attrs);
		ThumbnailManager.getInstance().registerImageReadyListener(this);
	}

	public LargeSubView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		ThumbnailManager.getInstance().registerImageReadyListener(this);
	}

	public void setMediaInfo(DataInfo info) {
		if (null == info || null == info.thumbnail_url)
			return;

		this.setImageBitmap(ThumbnailManager.getInstance().getImage(
			info, PictureType.PictureTypeThumbnail));

		if (null != ThumbnailManager.getInstance().getImage(
			info, PictureType.PictureTypeScreennail))
			this.setImageBitmap(ThumbnailManager.getInstance().getImage(
				info, PictureType.PictureTypeScreennail));
		_info = info;
	}

	@Override
	public void imageReady(String url) {
		if (null == _info
				|| (_info.thumbnail_url != url && _info.image_url != url))
			return;

		Message msg = new Message();
		msg.obj = url;
		_handler.sendEmptyMessage(0);
	};

	Handler _handler = new Handler() {
		@Override
		public void dispatchMessage(android.os.Message msg) {
			LargeSubView.this.setImageBitmap(ThumbnailManager.getInstance()
					.getImage(_info, PictureType.PictureTypeScreennail));
		};
	};
}
